//
// Created by zsj on 2019/12/22.
//

#ifndef SCLI_RSA_UTIL_H
#define SCLI_RSA_UTIL_H

#include <string>
#include <openssl/ossl_typ.h>
#include <openssl/rsa.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include "../comm/err_ctl.h"

using namespace std;

static int _rsa_keylen(const string& priv_key) {
    BIO *bio_key = BIO_new_mem_buf(priv_key.c_str(), -1);
    RSA *rsa = RSA_new();

    if (priv_key.find("-----BEGIN RSA") == 0) {
        // PKCS#1
        PEM_read_bio_RSAPrivateKey(bio_key, &rsa, NULL, NULL);
    } else if (priv_key.find("-----BEGIN PRIVATE") == 0) {
        // PKCS#8
        PEM_read_bio_RSAPrivateKey(bio_key, &rsa, NULL, NULL);
    } else {
        BIO_free(bio_key);
        __err("不支持的私钥格式。（支持PKCS#1和PKCS#8）");
    }

    return RSA_size(rsa);
}

#endif //SCLI_RSA_UTIL_H
